﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PowerTest.Admin
{
    public partial class frmSuaTTSV : Form
    {
        public frmSuaTTSV()
        {
            InitializeComponent();
        }
        string quyencu, pass;
        private void frmSuaTTSV_Load(object sender, EventArgs e)
        {
            try
            {
                // TODO: This line of code loads data into the 'powerTestDataSet.tblPhanQuyen' table. You can move, or remove it, as needed.
                this.tblPhanQuyenTableAdapter.Fill(this.powerTestDataSet.tblPhanQuyen);
                // TODO: This line of code loads data into the 'powerTestDataSet.viewUserSV' table. You can move, or remove it, as needed.
                this.viewUserSVTableAdapter.FillByMSSV(this.powerTestDataSet.viewUserSV, frmQLySV.masua.ToString());
                DateTime namsinh = DateTime.Parse((viewUserSVTableAdapter.GetDataByMSSV(frmQLySV.masua)).Rows[0][11].ToString());
                txtThang.Text = namsinh.Month.ToString();
                txtNgay.Text = namsinh.Day.ToString();
                txtNam.Text = namsinh.Year.ToString();
                quyencu = viewUserSVTableAdapter.GetDataByMSSV(frmQLySV.masua).Rows[0][4].ToString();
                pass = viewUserSVTableAdapter.GetDataByMSSV(txtMSSV.Text).Rows[0][1].ToString();        
            }
            catch { }
        }

        private void btXacNhan_Click(object sender, EventArgs e)
        {
            int i;
            try
            {
                i = 1;
                DateTime ns = DateTime.Parse(txtThang.Text + "/" +
                    txtNgay.Text + "/" + txtNam.Text);
            }
            catch
            {
                i = 0;
                lblThongBao.Text = "Ngày Tháng Năm sinh nhập không đúng!";
                lblThongBao.ForeColor = Color.Red;
                txtNgay.Focus();
            }

            if (i == 1)
            {
                if (txtHo.Text == "" || txtTen.Text == "" || txtLop.Text == "" ||
                    txtKhoa.Text == "" || txtNgay.Text == "" ||
                    txtThang.Text == "" || txtNam.Text == "" || cbGioiTinh.Text == "")
                {
                    lblThongBao.Text = "Phải nhập đầy đủ các ô!";
                    lblThongBao.ForeColor = Color.Red;
                }
                else
                {
                    if (MessageBox.Show("Lưu lại thông tin !", "Xac Nhan", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                    {
                        if (chkSua.Checked == true)
                        {
                            quyencu = cbQuyen.SelectedValue.ToString();
                        }
                        tblChiTietUserTableAdapter.UpdateQueryByID(txtHo.Text, txtDem.Text, txtTen.Text,
                                DateTime.Parse(txtThang.Text + "/" + txtNgay.Text + "/" + txtNam.Text),
                                cbGioiTinh.Text, txtDiaChi.Text, txtMSSV.Text, txtMSSV.Text);

                        tblSinhVienTableAdapter.UpdateQueryByMSSV(txtLop.Text, txtKhoa.Text,
                            txtMSSV.Text,quyencu,txtMSSV.Text,txtMSSV.Text);
                        Close();
                        new frmQLySV().Show();
                    }
                }
            }
        }

        private void chkSua_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSua.Checked == false)
            {
                gbQuyen.Enabled = false;
            }
            else 
            {
                gbQuyen.Enabled = true;                
            }
        }

        private void chkResetPass_CheckedChanged(object sender, EventArgs e)
        {

            if (chkResetPass.Checked == true)
            {
                pass = "123456";
            }
            else
            {
                pass = viewUserSVTableAdapter.GetDataByMSSV(txtMSSV.Text).Rows[0][1].ToString();
            }
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            Close();
            new frmQLySV().Show();
        }
    }
}
